
$(function(){


    $("#AddWarehouse").dialog({
        title:"添加",
        btnName:"添加",
        btnBind:function(){
            var params=$("#adWarehouse").serializeArray();
            var param="";
            for(var i in params){

                if(i==params.length-1){
                    param+=params[i].name+"="+params[i].value;
                }else{
                    param+=params[i].name+"="+params[i].value+"&";
                }
            }
            console.info(param)


            $.post("base/warehouse/addWarehouse",param,function (msg) {
                $("#AddWarehouse").dialog("close");
                toastr.warning("信息提示",msg);
                setTimeout(function () {
                    location.href='base/warehouse/index';
                },1000);
            },"text")
        }
    });
});


function addWarehouse(flg) {
    if(flg==1){
        $.ajax({
            url:'base/warehouse/getWarehouseCode',
            dataType:'json',
            type:'post',
            async:false,
            success:function (warehouse) {
                $("input[name='warehouseCode']").val(parseInt(warehouse.warehouseCode)+1);
            }
        });
        var option="<option value='100101'>东院区</option>";
        $("select[name='branchCode']").html(option);
        $("input[name='branchCode']").val("100101");
    }else {
        $.ajax({
            url:'base/warehouse/getWarehouseCode',
            dataType:'json',
            type:'post',
            async:false,
            success:function (warehouse) {
                $("input[name='warehouseCode']").val(parseInt(warehouse.warehouseCode)+1);
            }
        });
        var option="<option value='100102'>西院区</option>";
        $("select[name='branchCode']").html(option);
        $("input[name='branchCode']").val("100102");
    }
    $("#AddWarehouse").dialog("open");
}
function  updateWarehouse() {
    var params=$("#upWarehouse").serializeArray();
    var param="";
    for(var i in params){

        if(i==params.length-1){
            param+=params[i].name+"="+params[i].value;
        }else{
            param+=params[i].name+"="+params[i].value+"&";
        }
    }
    console.info(param)


    $.post("base/warehouse/updateWarehouse",param,function (msg) {
        toastr.warning("信息提示",msg);
    },"text")
}

function deleteWarehouse(id) {
    var param="id="+id;
    $.post("base/warehouse/delWarehouse",param,function (msg) {
        toastr.warning("信息提示",msg);
        setTimeout(function () {
            location.href='base/warehouse/index';
        },1000);

    },"text");
}

function searchByWarehouse(warehouseCode) {
    var param="warehouseCode="+warehouseCode;
    $.post("base/warehouse/getWarehouse",param,function (result) {
        $("#upWarehouse").setForm(result);

        var name=null;
        $.ajax({
            url:'sys/branch/searchByBranch',
            data:"id="+result.branchCode,
            dataType:'json',
            type:'post',
            async:false,
            success:function (branch) {
                name= branch.branchName;
            }
        });

       $("input[name='branchCode']").val(name)
        if(result.warehouseClass=='1'){
            $("input[name='warehouseClass']").val("一级库")
        }else{
            $("input[name='warehouseClass']").val("二级库")
        }

       console.info(result)
    },'json');

}